package discovery;

import lombok.extern.log4j.Log4j;

import java.io.*;
import java.net.Socket;
@Log4j
public class ServiceDiscoveryOld {
    private final String registryHost;
    private final int registryPort;

    public ServiceDiscoveryOld(String registryHost, int registryPort) {
        this.registryHost = registryHost;
        this.registryPort = registryPort;
    }

    public String discoverService(String serviceName) {
        try (Socket socket = new Socket(registryHost, registryPort);
             ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream input = new ObjectInputStream(socket.getInputStream())) {
            output.writeUTF(serviceName);
            output.writeUTF("lookup");
            output.flush();
            String response = input.readUTF();
            log.debug("服务发现结果:"+response);
            return response;
        } catch (IOException e) {
            log.error("发现服务失败:"+e.getMessage());
            return null;
        }
    }
}

